package com.four.shop.service.impl;

import com.four.shop.dao.AdderDao;
import com.four.shop.entity.Adder;
import com.four.shop.entity.Category;
import com.four.shop.service.AdderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author:wsy
 * @Date: 2021/12/9/15:35
 */
@Service
public class AdderServiceImpl implements AdderService {

    @Autowired
    private AdderDao adderDao;

    /**
     * 查询地址二级
     * @return
     */
    @Override
    public List<Adder> twoAdder() {
        //1.查询所有地址
        List<Adder> allList=adderDao.selectAll();
        List<Adder> adderOne=allList.stream().filter(item ->{
            return item.getSitParent().equals(0);
        }).map(item ->{
            //每一个item就是一个分类
            //设置二级分类
            //获取二级分类
            item.setChildren(getChildren(item.getSitId(),allList));
            return item;
        }).collect(Collectors.toList());
        return adderOne;
    }

    private List<Adder> getChildren(Integer sitId, List<Adder> allList) {
        List<Adder> child=allList.stream().filter(item ->{
            return item.getSitParent().equals(sitId);
        }).map(item ->{
            //item就是二级分类
            item.setChildren(getChildren(item.getSitId(),allList));
            return item;
        }).collect(Collectors.toList());
        return child;
    }
}
